package valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/4/11
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Byte> {

	private Set<Byte> set = new HashSet<>();

	/**
	 * 初始化方法
	 * @param constraintAnnotation constraintAnnotation
	 */
	@Override
	public void initialize(ListValue constraintAnnotation) {
		byte[] values = constraintAnnotation.values();
		for (byte value : values) {
			set.add(value);
		}
	}

	/**
	 *
	 * @param integer 需要检验的值
	 * @param constraintValidatorContext 上下文环境信息
	 * @return 校验结果
	 */
	@Override
	public boolean isValid(Byte integer, ConstraintValidatorContext constraintValidatorContext) {
		return set.contains(integer);
	}
}
